'''
# @Title:
# @Time : 2022/3/14 11:04
# @File : Hamcrest_Demo.py
# @Software: PyCharm

'''
from hamcrest import *


students_info = [
    {"name": "Zhang", "age": "18", "cash":23.05},
    {"name": "Wang", "age": "12", "cash":8.65},
    {"name": "Zhang", "age": "18", "cash":23.05}
]

class TestHamcrest:
    '''
    assert_that(actual, matcher=None, reason=""):

    :param actual: 实际结果
    :param matcher: 匹配器，期望匹配的结果
    :param reason: 校验失败后输出的信息（可选项）

    The exception raised for an unmet assertion is an
    :py:exc:`AssertionError`, which PyUnit reports as a test failure.

    :exception AssertionError
    '''

    '''
    匹配器：
    对象
        equal_to - 匹配相等的对象
        has_length - 匹配长度 len(item)
        has_property - 匹配属性值
        has_properties - 匹配包含所有属性值的对象
        has_string - 匹配字符串 str(item)
        instance_of - 匹配对象的类型
        none, not_none - 匹配None或not None
        same_instance - 匹配相同的对象
        calling, raises - calling和raise结合使用
    数字
        close_to - 匹配接近的数字
        greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to - 匹配数字大小
    文本
        contains_string - 匹配包含字符串
        ends_with - 匹配以字符串结尾
        equal_to_ignoring_case - 匹配完整字符串且忽略大小写
        equal_to_ignoring_whitespace - 匹配完整的字符串且忽略空白字符
        starts_with - 匹配以字符串开始
        string_contains_in_order- 在相对位置，匹配部分字符如string_contains_in_order("bc", "fg", "jkl")将会匹配abcdefghijklm
    逻辑
        all_of - 匹配所有指定的匹配项 and
        any_of - 匹配其中任意一个匹配项 or
        anything - 匹配任何条件，等效于true
        is_not - 匹配相反的条件，如assert_that(cheese, is_not(equal_to(smelly)))
        not_ - 等效于is_not 如 assert_that(alist, not_(has_item(item)))
    序列
        contains_exactly - 匹配完整的序列，排序也要一致
        contains_inanyorder - 匹配完整的序列，不要求排序一致
        has_item - 匹配序列中指定的元素
        has_items - 匹配序列中指定的多个元素，不要求排序
        is_in - 匹配元素是否在指定序列中
        only_contains - 匹配序列是否在指定list中出现
        empty - 匹配空序列
    字典 
        has_entries - 匹配字典多个键值对
        has_entry - 匹配字典键值对
        has_key - 匹配字典的key
        has_value - 匹配字典的value
    
    '''

    # equal_to
    def test_Name(self):
        assert_that(students_info[2].get("name"), equal_to("Zhang"), reason="该学生信息不存在")

    # same_instance
    def test_SameInstance(self):
        assert_that(students_info[2], same_instance(students_info[0]),reason="不是同一个对象")

    # instance_of
    def test_NameType(self):
        assert_that(students_info[0].get("name"), instance_of(str), reason="类型不同")

    '''
    close_to(value, delta)
    :param value 匹配值
    :param delta 上下浮动的范围
    '''
    def test_Cash(self):
        assert_that(7.65, close_to(value=students_info[1].get("cash"),delta=1), reason="金额相差过大")





